/*
 * @(#)SearchResultListener.java 0.0.1-SNAPSHOT
 *
 * Copyright Group B
 */

package grupb.upf.ms.listeners;

import grupb.upf.Controller.Controller;
import grupb.upf.ms.DescriptionContent;
import grupb.upf.ms.SearchResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

/**
 * <p>This component implements a PacketListener Interface. It receives
 * SearchResult messages and shows them to the user through the GUI.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public final class SearchResultListener implements
        org.jivesoftware.smack.PacketListener {

    /**
     * The logger. A new logger <code>grupb.upf.ms.SearchResultListener</code>
     * is created to add the logs of this class.
     */
    private static final Log LOG =
            LogFactory.getLog(SearchResultListener.class);
    /**
     * The controller to send the changes.
     */
    private Controller controller;

    /**
     * Constructor of the Listener.
     * @param c the controller to send the updates
     */
    public SearchResultListener(final Controller c) {
        this.controller = c;
    }
/**
 * Listens the petitions of other users.
 * @param packet The packets
 */
    @Override
    public void processPacket(final Packet packet) {
        // Put the incoming message on the chat history or chat board.
        final Message msg = (Message) packet;
        final String from = msg.getFrom();
        if (LOG.isInfoEnabled()) {
            final StringBuffer sb =
                    new StringBuffer(msg.getPacketID())
                    .append(" received from ").append(from);
            LOG.info(sb);
        }
        final DescriptionContent dc =
                ((SearchResult) msg.getProperty("SearchResult"))
                .getSearchResult();
        dc.addProperty("from", from);
        controller.searchResultReceived(dc);
    }
}
